package lhy.autoparts.servlet;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lhy.autoparts.pojo.Customer;
import lhy.autoparts.pojo.ItemInfo;
import lhy.autoparts.pojo.Supplier;
import lhy.orm.Serializer;
import lhy.orm.dao.DatabaseConnector;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


@WebServlet("/FkValueListServlet")
public class FkValueListServlet extends Utf8Servlet {
    @Override
    protected void doGet(HttpServletRequest que, HttpServletResponse pon) {
        super.doGet(que, pon);
        String table = que.getParameter("table");
        boolean isGenId = Boolean.parseBoolean(que.getParameter("isGenId"));
        boolean isSupplierOrCustomer = Supplier.class.getSimpleName().equals(table)|| Customer.class.getSimpleName().equals(table);
        boolean isItemInfo = ItemInfo.class.getSimpleName().equals(table);
        if(table.equals("String")) return;
        try {

            String sql;
            if(isItemInfo){
                //是商品信息，就返回商品信息一类的结果
                sql = "select * from ItemInfo ii join ItemCategory ic on ic.id = ii.cate_id join Supplier s on s.id = ii.supplier_id";
            }else if(isSupplierOrCustomer){
                //是供应商，就返回联系人电话地址一类的结果
                sql = "select id,name,contact,phone,address from " + table;
            }else if (isGenId) {
                //如果这个是自增id，就只获取id
                sql = "select id from " + table;
            } else {
                sql = "select id,name from " + table;
            }
            ResultSet rs=DatabaseConnector.preparedStatement(sql).executeQuery();

            List<String> idList = new ObjectArrayList<>();
            List<IdName> idNameList = new ObjectArrayList<>();
            while(rs.next()){
                String id = rs.getString("id");
                String val2Add = new String();
                if(isItemInfo){
                    //是商品信息
                    String iiid=rs.getString("ii.id");
                    String iiname=rs.getString("ii.name");
                    String model=rs.getString("model");
                    String icname=rs.getString("ic.name");
                    String sname = rs.getString("s.name").replace("科技有限公司","");
                    val2Add = String.format("%s-%s(%s),%s,(%s)",iiid,iiname,model,icname,sname);
                    idNameList.add(new IdName(id,val2Add));
                }else if(isSupplierOrCustomer){
                    //是供应商，就返回联系人电话地址一类的结果
                    val2Add+=rs.getString("name")+","+rs.getString("contact")+","+rs.getString("phone")+","+rs.getString("address").substring(0,3);
                    idNameList.add(new IdName(id,val2Add));
                }else if(isGenId){
                    idList.add(id);
                }

            }
            String json;
            if(idNameList.size()==0)
                json = Serializer.doSerialize(idList.toArray());
            else
                json = Serializer.doSerialize(idNameList.toArray());

            write(pon,json);
        } catch (SQLException e) {
            write(pon,e.getMessage());
            e.printStackTrace();
        }
    }
}
class IdName implements Serializable {
    String id;
    String name;

    public IdName(String id, String name) {
        this.id = id;
        this.name = name;
    }
    public String toString() {
        return Serializer.doSerialize(this);
    }
}